package com.oazon.moco.lan.handler;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.moco.launcher.LauncherApplication;
import com.moco.launcher.bean.MOCO_CONSTANTS;
import com.oazon.moco.bean.AuthType;
import com.oazon.moco.bean.DeviceLinkAuthParams;
import com.oazon.moco.bean.HttpMsgBodyBean;
import com.oazon.moco.bean.McServiceMsg;
import com.oazon.moco.bean.ProviderAdapt;
import com.oazon.moco.bean.SessionIdAuthParams;
import com.oazon.moco.lan.LanServerManage;
import com.oazon.moco.lan.core.AbsHttpMsgHandler;
import com.oazon.moco.lan.core.controldriver.cache.ControlDriverCache;
import com.oazon.moco.lan.core.http.bean.AVTransportUri;
import com.oazon.moco.lan.core.http.bean.DeleteBean;
import com.oazon.moco.lan.core.http.bean.FavoriteBean;
import com.oazon.moco.lan.core.http.bean.JsonMessageType;
import com.oazon.moco.lan.core.http.bean.RequestClassification;
import com.oazon.moco.lan.core.http.bean.RequestOperation;
import com.oazon.moco.lan.core.http.bean.Seek;
import com.oazon.moco.lan.core.http.bean.UriToQueue;
import com.oazon.moco.lan.core.http.bean.VolumeInstance;
import com.oazon.moco.lan.util.LanServer;
import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LanEventSource;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.playlist.PlayListManage;
import com.oazon.sonos.core.ISonosSoap;
import com.oazon.sonos.core.SonosControler;
import com.oazon.common.Assert;

public class ControlHandler extends AbsHttpMsgHandler {

	@Override
	public void doBusiness(HttpMsgBodyBean bean) {

		if (!RequestOperation.OPERATION_CONTROL.equals(bean.getOperation())) {
			return;
		}
		LinkSDK.getInstance().hasAction(LinkEventController.uiSource);

		// 如果没有订阅则不执行操作
		if (ControlDriverCache.getInstance().findControlDriverByUuid(
				bean.getUuid()) == null) {
			throw new NullPointerException("uuid:" + bean.getUuid());
		}
		// 播放器类
		if (RequestClassification.CLASSIFICATION_AUDIO.equals(bean
				.getClassification())) {
			// 播放列表的歌曲
			if (JsonMessageType.ADD_URI_TO_QUEUE.toString().equals(
					bean.getType())) {
				addUriToQueue(bean);
			}
			// 转换播放形式
			if (JsonMessageType.SET_AV_TRANSPORT_URI.toString().equals(
					bean.getType())) {
				setAvTransportUri(bean);
			}
			// 播放
			if (JsonMessageType.PLAY.toString().equals(bean.getType())) {
				play(bean);
			}
			// 暂停
			if (JsonMessageType.PAUSE.toString().equals(bean.getType())) {
				pause(bean);
			}
			// 停止
			if (JsonMessageType.STOP.toString().equals(bean.getType())) {
				stop(bean);
			}
			// 上一首
			if (JsonMessageType.PREVIOUS.toString().equals(bean.getType())) {
				previous(bean);
			}
			// 喜欢、收藏
			if (JsonMessageType.FAVORITE.toString().equals(bean.getType())) {
				favorite(bean);
			}
			// 删除
			if (JsonMessageType.DELETE.toString().equals(bean.getType())) {
				delete(bean);
			}
			// 下一首
			if (JsonMessageType.NEXT.toString().equals(bean.getType())) {
				next(bean);
			}
			// 播放模式
			if (JsonMessageType.PLAY_MODE.toString().equals(bean.getType())) {
				playMode(bean);
			}
		}
		// 系统级别的
		else if (RequestClassification.CLASSIFICATION_SYSTEM.equals(bean
				.getClassification())) {
			// rename Nsd Service
			if (JsonMessageType.RENAME.toString().equals(bean.getType())) {
				renameNsdServiceName(bean);
			}
		} else if (RequestClassification.CLASSIFICATION_RENDER.equals(bean
				.getClassification())) {
			// 设置进度
			if (JsonMessageType.SEEK.toString().equals(bean.getType())) {
				seek(bean);
			}

			// 播放设置音量
			else if (JsonMessageType.SET_VOLUME.toString().equals(bean.getType())) {
				serVolume(bean);
			}
		}else if(RequestClassification.CLASSIFICATION_SERVICE.equals(bean.getClassification())){
			if(JsonMessageType.ADD_SERVICE.toString().equals(bean.getType())){
				addservice(bean);
			}else if(JsonMessageType.REMOVE_SERVICE.toString().equals(bean.getType())){
				deleteService(bean);
			}
		}
	}

	private void deleteService(HttpMsgBodyBean bean) {
		
		JSONObject jsonObject = JSON.parseObject(bean.getData());
		String name = (String) jsonObject.get("name");
		LanEventSource source = new LanEventSource();
		source.setUuid(bean.getUuid());
		source.setChannelContext(bean.getCtx());
		LinkSDK.getInstance().serviceDelete(source, name);
	}

	private void addservice(HttpMsgBodyBean bean) {
		String addService = bean.getData();
		McServiceMsg object =	JSON.parseObject(addService, new TypeReference<McServiceMsg<SessionIdAuthParams>>(){});
		if(AuthType.DEVICE_LINK.value().equals(object.getAuthType())){
			object = JSON.parseObject(addService, new TypeReference<McServiceMsg<DeviceLinkAuthParams>>(){});
		}
		if(object == null){
			LanServer.responseResult(bean.getType(), -1, null, bean.getCtx());
			return;
		}
		//通知Linksdk --> 界面添加页面
		
		object.setId(ProviderAdapt.name2id(object.getName()));
		object.setUuid(bean.getUuid());
		LanEventSource eventSource = new LanEventSource();
		eventSource.setUuid(bean.getUuid());
		LinkSDK.getInstance().serviceAdded(eventSource, object);
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void serVolume(HttpMsgBodyBean bean) {
		String strVolume = bean.getData();

		VolumeInstance volumeInstance = JSON.parseObject(strVolume,
				VolumeInstance.class);
		volumeInstance.getVolume().get(0)
				.setVal(volumeInstance.getVolume().get(0).getVal() * 32 / 100);
		LinkSDK.getInstance().setVolume(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				volumeInstance);
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void seek(HttpMsgBodyBean bean) {
		String strSeek = bean.getData();
		Seek seek = JSON.parseObject(strSeek, Seek.class);

		LinkSDK.getInstance().setProgress(
				new LanEventSource(bean.getCtx(), bean.getUuid()), seek);
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void playMode(HttpMsgBodyBean bean) {
		String playMode = bean.getData();
		LinkSDK.getInstance().setPlayMode(
				new LanEventSource(bean.getCtx(), bean.getUuid()), playMode);
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void next(HttpMsgBodyBean bean) {
		LinkSDK.getInstance().next(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				getInstanceID(bean.getData()));
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void delete(HttpMsgBodyBean bean) {
		String strDeleteOrder = bean.getData();
		DeleteBean deleteOrder = JSON.parseObject(strDeleteOrder,
				DeleteBean.class);

		LinkSDK.getInstance().delete(
				new LanEventSource(bean.getCtx(), bean.getUuid()), deleteOrder);
	}

	private void favorite(HttpMsgBodyBean bean) {
		String strFavoriteOrder = bean.getData();
		FavoriteBean favoriteOrder = JSON.parseObject(strFavoriteOrder,
				FavoriteBean.class);

		LinkSDK.getInstance().favorite(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				favoriteOrder);
	}

	private void previous(HttpMsgBodyBean bean) {
		LinkSDK.getInstance().previous(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				getInstanceID(bean.getData()));
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void stop(HttpMsgBodyBean bean) {
		LinkSDK.getInstance().stop(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				getInstanceID(bean.getData()));
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void pause(HttpMsgBodyBean bean) {
		LinkSDK.getInstance().pause(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				getInstanceID(bean.getData()));
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void play(HttpMsgBodyBean bean) {
		JSONObject object = JSON.parseObject(bean.getData());
		int speed = object.getIntValue("speed");
		LinkSDK.getInstance().play(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				getInstanceID(bean.getData()), speed);
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void setAvTransportUri(HttpMsgBodyBean bean) {
		String strAVTransportUri = bean.getData();
		AVTransportUri avTransportUri = JSON.parseObject(strAVTransportUri,
				AVTransportUri.class);
		LinkSDK.getInstance().setAVTransportUri(
				new LanEventSource(bean.getCtx(), bean.getUuid()),
				avTransportUri);
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

	private void addUriToQueue(HttpMsgBodyBean bean) {
		String strUriToQueue = bean.getData();
		UriToQueue uriToQueues = JSON.parseObject(strUriToQueue,
				UriToQueue.class);
		LinkSDK.getInstance().addUriToQueue(
				new LanEventSource(bean.getCtx(), bean.getUuid()), uriToQueues);
	}
	
	private void renameNsdServiceName(HttpMsgBodyBean bean) {
		String strNewNsdServName = bean.getData();
		Assert.notNEWS(strNewNsdServName);
		
		LanServerManage.getInstance().resetNsdServiceName(strNewNsdServName);
		LanServer.responseResult(bean.getType(), 0, null, bean.getCtx());
	}

}
